local dawu = fk.CreateSkill {

  name = "joy__dawu",

  tags = {  },

}



dawu:addEffect(fk.EventPhaseStart, {
  name = "joy__dawu",
  anim_type = "defensive",
  events = {fk.EventPhaseStart, fk.DamageInflicted},
  expand_pile = "star",
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and  player:hasSkill(dawu.name)  and player.phase == Player.Finish and #player:getPile("star") > 0
    else
      return target == player and player:getMark("@@joy__dawu") > 0 and data.damageType == fk.NormalDamage
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local room = player.room
      local cids = room:askForCard(player, 1, 1, false, dawu.name, true, ".|.|.|star", "#joy__dawu-card", "star")
      if #cids > 0 then
        event:setCostData(self, {player, cids})
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local room = player.room
      room:moveCardTo(event:getCostData(self)[2], Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, dawu.name, "star")
        room:addPlayerMark(player, "@@joy__dawu")
    else
      data.damage = data.damage - 1
    end
  end,
})
dawu:addEffect(fk.DamageInflicted, {
  name = "joy__dawu",
  anim_type = "defensive",
  events = {fk.EventPhaseStart, fk.DamageInflicted},
  expand_pile = "star",
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and  player:hasSkill(dawu.name)  and player.phase == Player.Finish and #player:getPile("star") > 0
    else
      return target == player and player:getMark("@@joy__dawu") > 0 and data.damageType == fk.NormalDamage
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local room = player.room
      local cids = room:askForCard(player, 1, 1, false, dawu.name, true, ".|.|.|star", "#joy__dawu-card", "star")
      if #cids > 0 then
        event:setCostData(self, {player, cids})
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local room = player.room
      room:moveCardTo(event:getCostData(self)[2], Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, dawu.name, "star")
        room:addPlayerMark(player, "@@joy__dawu")
    else
      data.damage = data.damage - 1
    end
  end,
})
dawu:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@joy__dawu") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@@joy__dawu")
  end,
})

return dawu